package com.guigu.buy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.buy.dto.CellUtils;
import com.guigu.buy.pojo.OutDetails;
import com.guigu.buy.pojo.Stock;
import com.guigu.buy.pojo.StockDetails;
import com.guigu.buy.mapper.StockDetailsMapper;
import com.guigu.buy.pojo.StorageDetails;
import com.guigu.buy.service.IStockDetailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.buy.service.IStockService;
import com.guigu.buy.vo.CellVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author com.guigu
 * @since 2024-05-15
 */
@Service
public class StockDetailsServiceImpl extends ServiceImpl<StockDetailsMapper, StockDetails> implements IStockDetailsService {
    @Autowired
    private IStockService stockService;
    @Autowired
    private StockDetailsMapper stockDetailsMapper;
    @Override
    public void add(List<StorageDetails> list) {
        for (StorageDetails s:list){
            StockDetails stockDetails=new StockDetails();
            Stock stock = stockService.getById(s.getGatherId());
            int a=stock.getStockAmount()+s.getStorageTotal();
            stock.setStockAmount(a);
            stockDetails.setStockId(s.getGatherId());
            stockDetails.setProductId(s.getProductId());
            stockDetails.setSupplierId(s.getSupplierId());
            stockDetails.setAmount(s.getAmount());
            stockDetails.setRealAmount(s.getStorageTotal());
            stockDetails.setStats("0");
            this.save(stockDetails);
            stockService.updateById(stock);
        }
    }

    @Override
    public PageInfo<CellUtils> queryAll(int pageNo, int pageSize, CellVo vo) {
        List<CellUtils> list=stockDetailsMapper.query(vo);
        PageHelper.startPage( pageSize,pageNo);
        PageInfo<CellUtils> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void out(List<OutDetails> list) {
        for (OutDetails o:list){
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("product_id",o.getProductId());
            wrapper.eq("stock_id",o.getGatherId());
            System.out.println(o);
            StockDetails stockDetails=this.getOne(wrapper);
            stockDetails.setStockId(o.getGatherId());
            int stockDetailNum=stockDetails.getRealAmount()-o.getAmount();
            Stock stock = stockService.getById(o.getGatherId());
            int stockNum=stock.getStockAmount()-o.getAmount();
            stock.setStockAmount(stockNum);
            stockService.updateById(stock);
            this.updateById(stockDetails);
        }
    }

}
